package com.lql.lqlreader.zhihu.view

import `in`.srain.cube.views.ptr.PtrDefaultHandler
import `in`.srain.cube.views.ptr.PtrFrameLayout
import `in`.srain.cube.views.ptr.header.StoreHouseHeader
import `in`.srain.cube.views.ptr.util.PtrLocalDisplay.dp2px
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lql.lqlreader.MainActivity
import com.lql.lqlreader.R
import com.lql.lqlreader.util.startBrowserActivity
import com.lql.lqlreader.widget.LoadingDialog
import com.lql.lqlreader.widget.MyLoadMoreView
import com.lql.lqlreader.zhihu.model.ZhiHuDaily
import com.lql.lqlreader.zhihu.model.ZhiHuDailyDetail
import com.lql.lqlreader.zhihu.model.ZhiHuDailyItem
import com.lql.lqlreader.zhihu.model.fetchZhiHuDailyDetailById
import com.lql.lqlreader.zhihu.model.fetchZhiHuDailyListByDate
import kotlinx.android.synthetic.main.activity_zhihu_daily.*
import org.joda.time.DateTime
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import timber.log.Timber

class ZhiHuDailyFragment : Fragment() {
    private val mMostDate = 7 // 界面最大显示的子项
    private val mDateList = Array(7) { "" } // 日期列表
    private var mDatePosition = 0
    private lateinit var mAdapter: ZhiHuDailyAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.activity_zhihu_daily, container, false)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initDateList() // 初始化日期列表
        initView() // 初始化界面
        fetchZhiHuDailyList(mDateList[0]) // 抓取数据列表
    }

    private fun updateAdapter(zhiHuDailyItemList: List<ZhiHuDailyItem>) {
        if (mDatePosition == 0) {
            mAdapter.setNewData(zhiHuDailyItemList)
        } else {
            mAdapter.addData(zhiHuDailyItemList)
        }
    }

    private fun initView() {
        initPtr()
        initAdapter()
    }

    private fun initPtr() {
        initHeadView()
        ptrFrameOfZhiHuDaily.disableWhenHorizontalMove(true)
        ptrFrameOfZhiHuDaily.setPtrHandler(object : PtrDefaultHandler() {
            override fun onRefreshBegin(frame: PtrFrameLayout?) {
                mDatePosition = 0
                fetchZhiHuDailyList(mDateList[mDatePosition])
            }

            override fun checkCanDoRefresh(frame: PtrFrameLayout?, content: View?, header: View?): Boolean {
                return !recyclerViewRiBao.canScrollVertically(-1) && (activity as MainActivity).appBarLayoutVerticalOffset >= 0
            }
        })
    }

    private fun initHeadView() {
        val header = StoreHouseHeader(context) // 刷新控件
        header.setTextColor(resources.getColor(R.color.colorTxtSelected))
        header.setPadding(0, dp2px(16f), 0, 0)
        header.initWithString("Drop down refresh ZhiHu list", 15)
        ptrFrameOfZhiHuDaily.headerView = header
        ptrFrameOfZhiHuDaily.addPtrUIHandler(header)
    }

    private fun initAdapter() {
        mAdapter = ZhiHuDailyAdapter(R.layout.item_zhihu_daily, null)
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN)
        mAdapter.setLoadMoreView(MyLoadMoreView())
        mAdapter.setOnItemChildClickListener { adapter, _, position ->

            var loadingDialog: LoadingDialog? = null
            this@ZhiHuDailyFragment.context?.also {
                loadingDialog = LoadingDialog(it)
                loadingDialog?.show()
            }

            val item: ZhiHuDailyItem? = adapter.getItem(position) as ZhiHuDailyItem
            item?.also {
                fetchZhiHuDailyDetailById(it.id)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(object : Subscriber<ZhiHuDailyDetail>() {
                            override fun onCompleted() {
                                Timber.i("fetchZhiHuDailyDetailById onCompleted")
                                loadingDialog?.dismiss()
                            }

                            override fun onError(e: Throwable) {
                                Timber.e(e)
                                loadingDialog?.dismiss()
                            }

                            override fun onNext(storyDetail: ZhiHuDailyDetail) {
                                Timber.i("fetchZhiHuDailyDetailById onNext")
                                loadingDialog?.dismiss()
                                startBrowserActivity(context!!, storyDetail.share_url, storyDetail.title)
                            }
                        })
            }
        }
        mAdapter.setOnLoadMoreListener({
            if (mDatePosition < mMostDate - 1) {
                fetchZhiHuDailyList(mDateList[++mDatePosition])
            } else {
                mAdapter.loadMoreEnd()
            }
        }, recyclerViewRiBao)

        recyclerViewRiBao.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recyclerViewRiBao.adapter = mAdapter
    }

    private fun initDateList() {
        val now = DateTime.now().plusDays(1) // 今天的日期+1
        for (i in 0 until mMostDate) {
            val tempDateTime = now.minusDays(i)
            val year = tempDateTime.year.toString()
            val month = if (tempDateTime.monthOfYear < 10) "0" + tempDateTime.monthOfYear else tempDateTime.monthOfYear.toString()
            val day = if (tempDateTime.dayOfMonth < 10) "0" + tempDateTime.dayOfMonth else tempDateTime.dayOfMonth.toString()
            mDateList[i] = year + month + day
        }
    }

    private fun fetchZhiHuDailyList(date: String) { // 抓取知乎数据列表
        fetchZhiHuDailyListByDate(date)
                .subscribeOn(Schedulers.newThread())
                .filter { zhiHuDaily -> // 对日期数据加入-
                    for (item in zhiHuDaily.stories) {
                        item.date = StringBuilder(zhiHuDaily.date)
                                .insert(4, "-")
                                .insert(7, "-")
                                .toString()
                    }
                    true
                }.observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Subscriber<ZhiHuDaily>() {
                    override fun onNext(item: ZhiHuDaily?) {
                        ptrFrameOfZhiHuDaily?.refreshComplete()
                        if (item != null) {
                            updateAdapter(item.stories) // 更新适配器
                        }
                    }

                    override fun onCompleted() {
                        mAdapter.loadMoreComplete()
                    }

                    override fun onError(e: Throwable?) {
                        ptrFrameOfZhiHuDaily?.refreshComplete()
                        e?.printStackTrace()
                        Timber.e(e)
                    }
                })
    }
}